package com.exam.FaceLogin.controller;


import com.exam.FaceLogin.bean.Face;
import com.exam.FaceLogin.bean.result.FaceResult;
import com.exam.FaceLogin.mapper.FaceMapper;
import com.exam.FaceLogin.service.FaceService;
import com.exam.admin.service.AdminService;
import com.exam.anno.FaceLog;
import com.exam.entity.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author tanyongpeng
 * <p>des</p>
 **/
@RestController
@RequestMapping("/face")
@Slf4j
public class FaceController {

    @Autowired
    private FaceService faceService;

    @Autowired
    private AdminService adminService;

    /**
     * 人脸验证
     * @param imageBase,根据传入的base64编码和数据的base64编码进行对比
     * @return
     */
    @PostMapping("/vef")
    @FaceLog
    public FaceResult faceVef(@RequestBody String imageBase) {
        return faceService.vef(imageBase);
    }

    /**
     * 查询所有的人脸信息
     * @param current
     * @param size
     * @param faceName
     * @param faceStatus
     * @return
     */
    @GetMapping("/faceList")
    public ApiResult faceList(@RequestParam Integer current, @RequestParam Integer size,
                              @RequestParam(required = false) String faceName, @RequestParam(required = false) String faceStatus) {

        return faceService.faceList(current, size, faceName, faceStatus);
    }

    /**
     * 根据id进行删除人脸
     * @param fid
     * @return
     */
    @Transactional
    @GetMapping("/faceDelete/{fid}")
    public FaceResult faceDelete(@PathVariable Integer fid) {
        faceService.removeById(fid);
        adminService.deleteByFaceIdAdmin(fid);
        return FaceResult.success("删除成功");
    }

    /**
     * 根据id查询人脸信息
     * @param fid
     * @return
     */
    @GetMapping("/info/{fid}")
    public FaceResult info(@PathVariable Integer fid) {
        return FaceResult.success(faceService.getById(fid));
    }

    /**
     * 添加人脸
     * @param face
     * @return
     */
    @Transactional
    @PostMapping("/save")
    public FaceResult save(@RequestBody Face face) {
        face.setVefNum(0);
        face.setCreateTime(new Date());

        faceService.save(face);
        adminService.insertAdmin(face);

        return FaceResult.success("添加成功");
    }

    /**
     * 修改人脸
     * @param face
     * @return
     */
    @Transactional
    @PostMapping("/update")
    public FaceResult update(@RequestBody Face face) {
        faceService.updateById(face);
        adminService.updateAdmin(face);
        return FaceResult.success("修改成功");
    }


}
